package com.groupbuying.cheetah.order.storeFlow;

import com.groupbuying.cheetah.common.PageUtils;
import com.groupbuying.cheetah.common.Query;
import com.groupbuying.cheetah.utils.DesensitizationUtils;
import com.groupbuying.cheetah.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* 够样优店管理后台---o2o----订单流水管理*/
@RestController
@RequestMapping("/order")
public class StoreOrderFlowController {
  private static final Logger logger = LoggerFactory.getLogger(StoreOrderFlowController.class);

  @Autowired
  private StoreOrderFlowDAO storeOrderFlowDAO;

  @GetMapping("/list")
  public PageUtils list(@RequestParam Map<String, Object> params) {

    String payType = (String) params.get("pay_type");
    List<String> list = new ArrayList();
    if("现金支付".equals(payType)){
      list.add("微信支付");
      list.add("支付宝支付");
    }else if(StringUtils.isEmpty(payType)){
      list.add("微信支付");
      list.add("支付宝支付");
      list.add("积分支付");
      list.add("余额支付");
    }else{
      list.add(payType);
    }
    params.remove("pay_type");
    params.put("payTypes",list);
    Query query = new Query(params);
    logger.error("StoreOrderFlowController.list()-->接受的参数信息:"+ params.toString());
    List<Map<String, String>> storeOrderFlowList = storeOrderFlowDAO.storeOrderFlow(query);
//    if(storeOrderFlowList.size() > 0 && storeOrderFlowList != null){
//      for(Map<String, String> map:storeOrderFlowList){
//        String customerMobile = map.get("customer_mobile");
//        map.put("customer_mobile", DesensitizationUtils.desensitizePhoneNumber(customerMobile));
//      }
//    }
    int total = storeOrderFlowDAO.count(params);
    PageUtils pageUtils = new PageUtils(storeOrderFlowList, total);
    logger.error("StoreOrderFlowController.list()-->从pageUtils返回信息为:"+ pageUtils.getRows());
    logger.error("StoreOrderFlowController.list()-->从pageUtils返回信息为:"+ pageUtils.getTotal());
    return pageUtils;

  }


  @GetMapping("/export")
  public void refundManagement(@RequestParam Map<String, Object> params, HttpServletResponse response) {


    Query query = new Query(params);
    List<Map<String, String>> storeMoneyFlowList = storeOrderFlowDAO.storeOrderFlow(query);
    try {
     // String[] header = { "订单编号", "买家名称","门店名称", "运营专员","下单时间","现金支付", "零钱支付","订单总金额", "支付方式","订单类型","支付状态"};
      //Object[] columns = { "orderId", "customerId","storeName","operationSpecialist","orderTime","cashPayment","small_change","grossAmount","payType","order_type","order_state"};
      //ExcelUtils.writeExcelByObject(storeMoneyFlowList, response, "商户流水账单", header, columns);
    } catch (Exception e) {
      e.printStackTrace();

    }
  }

  @GetMapping("/amountStatistics")
  public ApiResult amountStatistics(@RequestParam Map<String, Object> params) {
    try{
      logger.info("StoreOrderFlowController.amountStatistics()"+params);
      Map<String, String> result = storeOrderFlowDAO.amountStatistics(params);
      logger.info("StoreOrderFlowController.amountStatistics()"+result);
      if(result == null || result.size()<=0){
        result = new HashMap();
        result.put("payMoney","0");
        result.put("slottingAllowance","0");
        result.put("platformProfit","0");
      }
      return ApiResult.success(result);
    }catch(Exception e){
      logger.error("StoreOrderFlowController.amountStatistics()",e);
      return ApiResult.failure();
    }
  }

}
